package com.zhudoo.zhilian.dao.impl;

import com.zhudoo.zhilian.dao.entity.Tip;
import com.zhudoo.zhilian.dao.repository.TipRepository;
import com.zhudoo.zhilian.service.TipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhudoo.zhilian.service.TipService.ASE;

@Component
public class TipDao {
    @Autowired
    TipRepository tipRepository;

    @PersistenceContext
    private EntityManager manager;

    public Tip getTipByOnlyStr(String onlyStr) {
        return tipRepository.findByValidAndStatusAndOnlyStr(true, TipService.TIP_STATUS_1, onlyStr);
    }

    public List<Tip> getTipByUserIdAndValid(Integer userId, boolean valid, int page, int limit) {
        Pageable pageable = PageRequest.of(page - 1, limit, Sort.by("createTime").descending());
        return tipRepository.findByUserIdAndValid(userId, valid, pageable);
    }

    public List<Tip> getTipList(int page, int limit, Map<String, Object> parameter) {

        Map<String, Object> newParameter = new HashMap<>(8);
        String sql = "from Tip";
        sql += " where valid=1 and status=" + TipService.TIP_STATUS_1;
        if (parameter.get("cityId") != null) {
            sql += " and cityId=:cityId";
            newParameter.put("cityId", parameter.get("cityId"));
        }
        if (parameter.get("tipType") != null && (int) parameter.get("tipType") != 0) {
            sql += " and type=:tipType";
            newParameter.put("tipType", parameter.get("tipType"));
        }
        if (parameter.get("roomType") != null && (int) parameter.get("roomType") != 0) {
            sql += " and roomType=:roomType";
            newParameter.put("roomType", parameter.get("roomType"));
        }
        if (parameter.get("line") != null && (int) parameter.get("line") != 0) {
            sql += " and line=:line";
            newParameter.put("line", parameter.get("line"));
        }
        if (parameter.get("station") != null && (int) parameter.get("station") != 0) {
            sql += " and station=:station";
            newParameter.put("station", parameter.get("station"));
        }
        if (parameter.get("hall") != null && (int) parameter.get("hall") != 0) {
            sql += " and hall=:hall";
            newParameter.put("hall", parameter.get("hall"));
        }
        if (parameter.get("startPrice") != null) {
            sql += " and price>=:startPrice";
            newParameter.put("startPrice", parameter.get("startPrice"));
        }
        if (parameter.get("endPrice") != null) {
            sql += " and price<=:endPrice";
            newParameter.put("endPrice", parameter.get("endPrice"));
        }
        if ((int) parameter.get("checkInDateSort") != 0 && (int) parameter.get("priceSort") != 0) {
            if (ASE == (int) parameter.get("checkInDateSort")) {
                sql += " order by checkInDate asc";
            } else {
                sql += " order by checkInDate desc";
            }
            if (ASE == (int) parameter.get("priceSort")) {
                sql += "  ,price asc";
            } else {
                sql += "  ,price desc";
            }
        } else if ((int) parameter.get("checkInDateSort") != 0) {
            if (ASE == (int) parameter.get("checkInDateSort")) {
                sql += " order by checkInDate asc";
            } else {
                sql += " order by checkInDate desc";
            }
        } else if ((int) parameter.get("priceSort") != 0) {
            if (ASE == (int) parameter.get("priceSort")) {
                sql += " order by price asc";
            } else {
                sql += " order by price desc";
            }
        } else {
            sql += " order by updateTime desc";
        }

        Query query = manager.createQuery(sql, Tip.class);
        for (Map.Entry<String, Object> entry : newParameter.entrySet()) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
        query.setFirstResult(limit * (page - 1));
        query.setMaxResults(limit);
        return query.getResultList();
    }

    public Tip getTipById(Integer id) {
        return tipRepository.getOne(id);
    }

    public void saveTip(Tip tip) {
        tipRepository.save(tip);
    }

    /**
     * 更新浏览次数
     *
     * @param tipId
     * @param viewCount
     */
    @Transactional(rollbackFor = Exception.class)
    public void addViewCount(Integer tipId, int viewCount) {
        System.out.println();
        tipRepository.addViewCount(tipId, viewCount);
    }


}
